package com.pounds.service.impl;

import com.pounds.mapper.UserMapper;
import com.pounds.pojo.User;
import com.pounds.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    enum UserEnumError {
        Normal,
        DataFormatError,
        EmptyError,
        DataNotFoundError
    }

    @Override
    public List<User> getUserList() {
        return userMapper.getUserList();
    }

    @Override
    public String addUser(String name, String sex, int age, String address, String contact, String admin, String link, String need, String note) {
        if (name == null || sex == null || address == null || age < 0 || age > 200 || contact == null || admin == null || link == null) {
            return UserEnumError.DataFormatError.toString();
        } else if (name.isEmpty() || sex.isEmpty() || address.isEmpty() || contact.isEmpty() || admin.isEmpty() || link.isEmpty()) {
            return UserEnumError.EmptyError.toString();
        }
        userMapper.addUser(name, sex, age, address, contact, admin, link, need, note);
        return UserEnumError.Normal.toString();
    }

    @Override
    public String deleteUser(int id) {
        List<User> user = userMapper.selectUser(id);
        if (user == null) {
            return UserEnumError.DataNotFoundError.toString();
        }
        userMapper.deleteUser(id);
        return UserEnumError.Normal.toString();
    }

    @Override
    public String updateUser(int id, String name, String sex, int age, String address, String contact, String admin, String link, String need, String note) {
        if (name == null || sex == null || address == null || age < 0 || age > 200 || contact == null || admin == null || link == null) {
            return UserEnumError.DataFormatError.toString();
        } else if (name.isEmpty() || sex.isEmpty() || address.isEmpty() || contact.isEmpty() || admin.isEmpty() || link.isEmpty()) {
            return UserEnumError.EmptyError.toString();
        }
        userMapper.selectUser(id);
        List<User> user = userMapper.selectUser(id);
        if (user == null) {
            return UserEnumError.DataNotFoundError.toString();
        }
        userMapper.updateUser(id, name, sex, age, address, contact, admin, link, need, note);
        return UserEnumError.Normal.toString();
    }
}
